package dk.ihk.shopular.services;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import dk.ihk.shopular.models.ShoppingItem;
import dk.ihk.shopular.models.ShoppingList;

public class ShoppingService {
	@Autowired
	private UserService userService;
	
	private List<ShoppingList> shoppingListPool = new ArrayList<ShoppingList>();
	
	public List<ShoppingList> getShoppingListPool() {
		return shoppingListPool;
	}
	
	/**
	 * returns the list with the given name, otherwise returns null
	 * @param name
	 * @return
	 */
	public ShoppingList getShoppingListByName(String name) {
		for (ShoppingList list : getShoppingListPool()) {
			if (list.getName().equals(name)) return list;
		} 
		return null;
	}
	
	/**
	 * adds an item to a shopping-list
	 * @param item
	 * @param listName
	 */
	public void addItemToShoppingList(ShoppingItem item, String listName) {
		ShoppingList list = getShoppingListByName(listName);
		if (list != null) {
			list.addItem(item);
		}
	}
	
	/**
	 * add a newly created list to the shopping list pool
	 * @param list
	 */
	public void addToShoppingListPool(ShoppingList list) {
		shoppingListPool.add(list);
	}
	
	public List<ShoppingList> getUsersShoppingLists(HttpServletRequest request) {
		return userService.getCurrentUser(request).getShoppingLists();
	}
}
